//
//  TableViewCellV2.m
//  樓盤王
//
//  Created by Jacky Mok on 12/12/2017.
//  Copyright © 2017 OR Media Limited. All rights reserved.
//

#import "TableViewCellV2.h"
#import "AppDelegate.h"
#import "TableViewCellV2Toolbar.h"
@implementation TableViewCellV2

-(id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        img = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height-80)];
        [self addSubview:img];
        bg = [[UIView alloc] initWithFrame:CGRectMake(0,self.frame.size.height-120,delegate.screenWidth/2,40)];
        [bg setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
        [self addSubview:bg];
        
        t = [[UILabel alloc] initWithFrame:CGRectMake(10,10,delegate.screenWidth/2-20,20)];
        [t setFont:[UIFont boldSystemFontOfSize:12]];
        [t setTextColor:[UIColor whiteColor]];
        [bg addSubview:t];
        
        price = [[TableViewCellV2Toolbar alloc] initWithFrame:CGRectMake(0,self.frame.size.height-80,self.frame.size.width,40)];
        [self addSubview:price];
        
        area = [[TableViewCellV2Toolbar alloc] initWithFrame:CGRectMake(0,self.frame.size.height-40,self.frame.size.width,40)];
        [self addSubview:area];
        
        numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setGroupingSeparator:@","];
        [numberFormatter setGroupingSize:3];
        [numberFormatter setUsesGroupingSeparator:YES];
        [numberFormatter setDecimalSeparator:@"."];
        [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
        [numberFormatter setMaximumFractionDigits:2];
        
        
        type = [[UIImageView alloc] initWithFrame:CGRectMake(10,self.frame.size.height-160,60,40)];
        [type setContentMode:UIViewContentModeScaleAspectFit];
        [self addSubview:type];
        
        excl = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-70,10,60,40)];
        [excl setContentMode:UIViewContentModeScaleAspectFit];
        [excl setImage:[UIImage imageNamed:@"exclusive_130x80.png"]];
        [self addSubview:excl];
        
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
        [tap setNumberOfTapsRequired:1];
        siteid =0;
        
        
        
        
    }
    return self;
}
-(void) tapped {
    if (siteid >0) {
        [delegate loadSite:siteid];
    }
}
-(void) drawWithData:(NSDictionary *)data {
    [type removeFromSuperview];
    [excl removeFromSuperview];
    [self removeGestureRecognizer:tap];
    if (data ==nil) {
        [img setHidden:YES];
        [t setHidden:YES];
        [price setHidden:YES];
        [area setHidden:YES];
        [bg setHidden:YES];
        return;
    } else {
        [img setHidden:NO];
        [t setHidden:NO];
        [price setHidden:NO];
        [area setHidden:NO];
        [bg setHidden:NO];
        [img addGestureRecognizer:tap];
        siteid = [[data objectForKey:@"id"] intValue];
    }
    
    [self setUserInteractionEnabled:YES];
    [img setUserInteractionEnabled:YES];
    [img setContentMode:UIViewContentModeScaleAspectFill];
    [img setClipsToBounds:YES];
    [delegate getImage:[[[data objectForKey:@"images"] objectAtIndex:0]objectAtIndex:0] andTag:0 callback:^(UIImage *image, int tag) {
        [img setImage:image];
    }];
    
    if ( [[data objectForKey:@"type"] isEqualToString:@"住宅"] ||
         [[data objectForKey:@"type"] isEqualToString:@"居屋"] ||
         [[data objectForKey:@"type"] isEqualToString:@"唐樓"] ||
         [[data objectForKey:@"type"] isEqualToString:@"套房"]
        ) {
        [type setImage:[UIImage imageNamed:@"house_130x60.png"]];
        [self addSubview:type];
    } else if ([[data objectForKey:@"type"] isEqualToString:@"商舖"] ||
                [[data objectForKey:@"type"] isEqualToString:@"寫字樓"] ||
                [[data objectForKey:@"type"] isEqualToString:@"工業大廈"]
                ) {
        [type setImage:[UIImage imageNamed:@"shop_130x60.png"]];
        [self addSubview:type];
    } else if ([[data objectForKey:@"type"] isEqualToString:@"車位"]) {
        [type setImage:[UIImage imageNamed:@"carpark_130x60.png"]];
        [self addSubview:type];
    }
    
    if ([[data objectForKey:@"exclusive"] isEqualToString:@"1"]) {
        [self addSubview:excl];
    }
    
    title = [data objectForKey:@"building_zh"];
    if (![@"" isEqualToString:[data objectForKey:@"title"]]) {
        title = [data objectForKey:@"title"];
    } else {
        if (![[data objectForKey:@"street_zh"] isEqualToString:@""]) {
            title = [title stringByAppendingString:[data objectForKey:@"street_zh"]];
        }
    }
 
    [t setText:title];
    
    
    
    //NSLog(@"%f %f CellV2",self.frame.size.width,self.frame.size.height);
    
    CGFloat v = [[data objectForKey:@"rent"] floatValue];
    CGFloat p = [[data objectForKey:@"price"] floatValue];
    [price setData:COLOR_PRICE andN1:[numberFormatter stringFromNumber:[NSNumber numberWithFloat:v]] andN2:[numberFormatter stringFromNumber:[NSNumber numberWithFloat:p]]];
    
    CGFloat aa = [[data objectForKey:@"area_actual"] floatValue];
    CGFloat ab = [[data objectForKey:@"area_build"] floatValue];
    [area setData:COLOR_AREA andN1:[numberFormatter stringFromNumber:[NSNumber numberWithFloat:aa]] andN2:[numberFormatter stringFromNumber:[NSNumber numberWithFloat:ab]]];
    
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
